package com.controller;

import com.data.HitCourse;
import com.data.page;
import com.service.CourseService;
import com.data.Course;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class CourseController extends HttpServlet {
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        req.setCharacterEncoding("utf-8");
        HttpSession session=req.getSession();
        int uId=((Integer)session.getAttribute("uId")).intValue();

        CourseService courseservice=new CourseService();

        String h="",s="";

        String pageNo=req.getParameter("pageNo");
        String pageSize=req.getParameter("pageSize");
        String No=req.getParameter("pageNo");
        System.out.println(pageNo);
        System.out.println(pageSize);
        if(pageNo==null||pageNo.equals("")){
            pageNo="1";
        }
        if(pageSize==null||pageSize.equals("")){
            pageSize="5";
        }
        page p=courseservice.getPage(Integer.valueOf(pageNo),Integer.valueOf(pageSize));
        session.setAttribute("page",p);
        String order=req.getParameter("order");
        if(order==null){
            session.setAttribute("cNameOrder",0);
            session.setAttribute("cNumOrder",0);
        }
        if(order!=null){
            if(order.equals("cName")){
                int i=Integer.valueOf(session.getAttribute("cNameOrder").toString());
                if(i%2==0){
                    p=courseservice.getPage(Integer.valueOf(pageNo),Integer.valueOf(pageSize),"order by cName");
                }
                else{
                    p=courseservice.getPage(Integer.valueOf(pageNo),Integer.valueOf(pageSize),"order by cName desc");
                }
                i++;
                session.setAttribute("cNameOrder",i);
            }
            else{
                int i=Integer.valueOf(session.getAttribute("cNumOrder").toString());
                if(i%2==0){
                    p=courseservice.getPage(Integer.valueOf(pageNo),Integer.valueOf(pageSize),"order by cNum");
                }
                else{
                    p=courseservice.getPage(Integer.valueOf(pageNo),Integer.valueOf(pageSize),"order by cNum desc");
                }
                i++;
                session.setAttribute("cNumOrder",i);
            }
            session.setAttribute("page",p);
            resp.sendRedirect("allCourse.jsp");
            return;
        }
        if(session.getAttribute("hitcourse")!=null){
         h=session.getAttribute("hitcourse").toString();
        }
        if(session.getAttribute("course")!=null){
            s=session.getAttribute("course").toString();
        }

        List<HitCourse> hitCourses=null;
        if(h.equals("display")){
            hitCourses=courseservice.Hitdisplay(uId);
            session.setAttribute("Hitresult",hitCourses);
            resp.sendRedirect("HitCourse.jsp");
            return;
        }
        if(h.equals("add")&&(No==null)){
            System.out.println("add");
            if(req.getParameterValues("allchoose")==null){
                resp.sendRedirect("HitCourse.jsp");
                return;
            }
            String[] adds=req.getParameterValues("allchoose");
            for(String add:adds){
                if(courseservice.checkcId(uId,Integer.valueOf(add))){
                    session.setAttribute("Hitfailure","Hitfailure");
                    resp.sendRedirect("allCourse.jsp");
                    return;
                }
            }
            for(String add:adds){
                courseservice.Hitadd(Integer.valueOf(session.getAttribute("uId").toString()),Integer.valueOf(add));
            }
            System.out.println("add");
            hitCourses=courseservice.Hitdisplay(uId);
            session.setAttribute("Hitresult",hitCourses);
            session.setAttribute("result",courseservice.display());
            resp.sendRedirect("HitCourse.jsp");
            return;
        }
        //System.out.println(session.getAttribute("hitcourse"));
        if(h.equals("delete")){
            if(req.getParameterValues("hitchoose")==null){
                //System.out.println("选择为空");
                resp.sendRedirect("HitCourse.jsp");
                return;
            }
            //System.out.println("退选");
            String[] hits=req.getParameterValues("hitchoose");
            for(String hit:hits){
                courseservice.Hitdelete(uId,Integer.valueOf(hit));
            }
            hitCourses=courseservice.Hitdisplay(uId);
            session.setAttribute("Hitresult",hitCourses);
            session.setAttribute("result",courseservice.display());
            resp.sendRedirect("HitCourse.jsp");
            return;
        }
        if(s.equals("add")){
            //String cid=session.getAttribute("acid").toString();
            String cName=req.getParameter("cName");
            String cNum=req.getParameter("cNum");
            String cType=req.getParameter("cType");

            String error=courseservice.failure(cName,cNum,cType);
            if(!error.isEmpty()) {
                req.getSession().setAttribute("courseFailure", error);
                resp.sendRedirect("courseFailure.jsp");
                return;
            }

            courseservice.add(cName,Integer.valueOf(cNum),cType);
        }
        if (s.equals("delete")&&No==null){
            if(req.getParameterValues("choose")==null){
                String fail="未选中课程";
                req.getSession().setAttribute("courseFailure",fail);
                resp.sendRedirect("courseFailure.jsp");
                return;
            }
            String[] del=req.getParameterValues("choose");
                    for(String de:del){
                        courseservice.delete(Integer.valueOf(de));
                    }
        }
        if(s.equals("delete")&&No!=null){
            resp.sendRedirect("delCourse.jsp");
            return;
        }
         p=courseservice.getPage(Integer.valueOf(pageNo),Integer.valueOf(pageSize));
        session.setAttribute("page",p);
        //resp.sendRedirect(req.getContextPath()+"/allCourse.jsp");
        //resp.addHeader("refresh","1;URL=/Homework-920106840130/allCourse.jsp");
        resp.sendRedirect("allCourse.jsp");
    }
}
